package dk.itu.mobile.client;

import java.rmi.RemoteException;

public class SOAPClient implements Clientable {
	private String _endpoint = null;
	private dk.itu.mobile.client.ITaskManagerService iTaskManagerService = null;

	public SOAPClient() {
		_initITaskManagerServiceProxy();
	}

	public SOAPClient(String endpoint) {
		_endpoint = endpoint;
		_initITaskManagerServiceProxy();
	}

	private void _initITaskManagerServiceProxy() {
		try {
			iTaskManagerService = (new dk.itu.mobile.client.util.TaskManagerServiceLocator()).getBasicHttpBinding_ITaskManagerService();
			if (iTaskManagerService != null) {
				if (_endpoint != null)
					((javax.xml.rpc.Stub)iTaskManagerService)._setProperty("javax.xml.rpc.service.endpoint.address", _endpoint);
				else
					_endpoint = (String)((javax.xml.rpc.Stub)iTaskManagerService)._getProperty("javax.xml.rpc.service.endpoint.address");
			}

		}
		catch (javax.xml.rpc.ServiceException serviceException) {}
	}

	public String getEndpoint() {
		return _endpoint;
	}

	public void setEndpoint(String endpoint) {
		_endpoint = endpoint;
		if (iTaskManagerService != null)
			((javax.xml.rpc.Stub)iTaskManagerService)._setProperty("javax.xml.rpc.service.endpoint.address", _endpoint);

	}

	public ITaskManagerService getITaskManagerService() {
		if (iTaskManagerService == null)
			_initITaskManagerServiceProxy();
		return iTaskManagerService;
	}

	public String GetAttendantTasks(java.lang.String attendantId) {
		if (iTaskManagerService == null)
			_initITaskManagerServiceProxy();
		String response = "";

		try {
			response = iTaskManagerService.getAttendantTasks(attendantId);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;

	}


	public String CreateTask(java.lang.String taskXml) {
		if (iTaskManagerService == null)
			_initITaskManagerServiceProxy();
		String response = "";

		try {
			response = iTaskManagerService.createTask(taskXml);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;

	}

	public String DeleteTask(java.lang.String taskId) {
		if (iTaskManagerService == null)
			_initITaskManagerServiceProxy();
		String response = "";

		try {
			response = iTaskManagerService.deleteTask(taskId);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;
	}

	public static void main(String[] args) {
    	Clientable testClient = new SOAPClient();
    	String testAttendant = "rao";
    	String testAttendant2 = "dianaross";
    	String testTaskXML = "<tasks><task id='club54' name='The End of Modern-day Gomorrah' date='04-02-1980' status='so-executed'><description>Closing party of Club 54</description><attendants>andywarhole, michaeljackson, dianaross, rao</attendants></task></tasks>";
    	String testTaskId = "club54";
    	
    	//Test get GetAttendantTasks method
    	String attendantsTasks =  testClient.GetAttendantTasks(testAttendant);
    	//Attendants task as xml
    	System.out.println(testAttendant + "'s  tasks: \n" + attendantsTasks);
    	System.out.println();
    	
    	//Test CreateTask method
    	String createTaskResult = testClient.CreateTask(testTaskXML);
    	System.out.println("createTaskResult: " + createTaskResult);
    	System.out.println();
    	
    	//Test on testAttendant2
    	attendantsTasks =  testClient.GetAttendantTasks(testAttendant2);
     	//Attendants task as xml
    	System.out.println(testAttendant2 + "'s  tasks: \n" + attendantsTasks);
    	System.out.println();
    	System.out.println();
    	
    	//Test CreateTask method
    	createTaskResult = testClient.CreateTask(testTaskXML);
    	System.out.println("createTaskResult: " + createTaskResult);
    	System.out.println();
    	
    	//Test on testAttendant2
    	attendantsTasks =  testClient.GetAttendantTasks(testAttendant2);
     	//Attendants task as xml
    	System.out.println(testAttendant2 + "'s  tasks: \n" + attendantsTasks);
    	System.out.println();
    	
    	//Test DeleteTask method
    	String deleteTaskResult = testClient.DeleteTask(testTaskId);
    	System.out.println("deleteTaskResult: " + deleteTaskResult);
    	System.out.println();
    	
    	//Test get GetAttendantTasks method to verify test calls to CreateTask and DeleteTask
    	attendantsTasks =  testClient.GetAttendantTasks(testAttendant);
     	//Attendants task as xml
    	System.out.println(testAttendant + "'s  tasks: \n" + attendantsTasks);
    	System.out.println();
    	
    	//Test on testAttendant2
    	attendantsTasks =  testClient.GetAttendantTasks(testAttendant2);
     	//Attendants task as xml
    	System.out.println(testAttendant2 + "'s  tasks: \n" + attendantsTasks);
    	System.out.println();
    	
    	
    }




}